package com.jae.controller.roleResc;


import com.jae.controller.BaseController;
import com.jae.model.ResultMsg;
import com.jae.model.ServiceRuntimeException;
import com.jae.model.roleResc.RoleResc;
import com.jae.model.user.User;
import com.jae.service.RoleRescService;
import com.jae.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 角色权限Controller
 * 
 * @author Jae
 * @email 1571878499@qq.com
 * @date 2020-12-01 09:22:32
 */
@RestController
@RequestMapping("/admin/roleResc")
public class RoleRescAdminController extends BaseController {

	@Autowired
	private RoleRescService roleRescService;
	/**
	 * /admin/RoleResc/all 所有角色权限列表
	 */
	@RequestMapping(value = "/all", method = RequestMethod.POST)
	public ResultMsg all(HttpServletRequest request, String role_id, String resc_id) {
		String interfaceName = "所有角色权限列表";
		try {
			Map map = new HashMap();
			if (StringUtil.hasTest(role_id))
				map.put("role_id", role_id.trim());
			if (StringUtil.hasTest(resc_id))
				map.put("resc_id", resc_id.trim());
			return new ResultMsg(true, roleRescService.findAll(map));
		} catch (ServiceRuntimeException e) {
			return fail(e);
		} catch (Exception e) {
			return error(interfaceName, e, request);
		}
	}

	/**
	 * /admin/roleResc/add 添加角色权限
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public ResultMsg add(HttpServletRequest request, RoleResc roleResc) {
		String interfaceName = "添加角色权限";
		try {
			User user = getUser();
			return new ResultMsg(true, roleRescService.insert(roleResc, user));
		} catch (ServiceRuntimeException e) {
			return fail(e);
		} catch (Exception e) {
			return error(interfaceName, e, request);
		}
	}

}
